Skip to content

perf: array_agg() performance improvements - #23716

Open
fred1268 wants to merge 8 commits into
apache:mainfrom
fred1268:perf/array_aggregate
Open

perf: array_agg() performance improvements#23716
fred1268 wants to merge 8 commits into
apache:mainfrom
fred1268:perf/array_aggregate

Conversation

@fred1268

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Queries using array_agg(DISTINCT col) were significantly slower than expected.

Profiling revealed that DistinctArrayAggAccumulator::update_batch was allocating a
heap-owned String on every single input row — even for rows whose value was already
present in the accumulator. For a typical low-cardinality workload (e.g. a column of ~25
database names across thousands of rows), this meant paying the full allocation cost for
every duplicate, which dominated the runtime.

What changes are included in this PR?

This PR applies the same deduplication strategy already used by AggregateExec for
GROUP BY: duplicate rows now cost only a hash probe with no heap allocation, and new
distinct values are appended to a single shared buffer rather than allocated individually.
The fix applies to all column types, not just strings, and retract_batch support
(required for sliding window frames such as ROWS BETWEEN N PRECEDING AND CURRENT ROW)
is fully preserved.

Are these changes tested?

Four unit tests were added to DistinctArrayAggAccumulator — one each for Utf8,
Int64, Float64, and Date32 — to pin the deduplication contract across the most
common column types and serve as a regression guard for future changes. The existing
sliding window sqllogictest suite (array_agg_sliding_window.slt) covers retract_batch
correctness end-to-end and passes unchanged.

Two update_batch micro-benchmarks were added to measure the before/after on realistic
data: one with low cardinality (~25 distinct database names in 8 192 rows, modelling the
common production case) and one with high cardinality (~7 800 distinct values, modelling
the worst case where almost every row is new). Results on an 8 192-row batch:

Benchmark Before After Speedup
Low cardinality (~25 distinct DB names) 648.6 µs 189.4 µs 3.42×
High cardinality (~7 800 distinct values) 1078.3 µs 269.4 µs 4.00×

Are there any user-facing changes?

No user-facing changes

No breaking changes to public APIs

@github-actions github-actions Bot added the functions Changes to functions implementation label Jul 20, 2026
@codecov-commenter

codecov-commenter commented Jul 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.53731% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.69%. Comparing base (1fcdef2) to head (1cd1305).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/functions-aggregate/src/array_agg.rs 92.53% 2 Missing and 8 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #23716      +/-   ##
==========================================
- Coverage   80.69%   80.69%   -0.01%     
==========================================
  Files        1089     1089              
  Lines      368525   368602      +77     
  Branches   368525   368602      +77     
==========================================
+ Hits       297392   297447      +55     
- Misses      53414    53426      +12     
- Partials    17719    17729      +10     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@gabotechs

Copy link
Copy Markdown
Contributor

run benchmarks array_agg

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5022949366-1176-hk9sf 6.12.85+ #1 SMP Mon May 11 08:17:35 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing perf/array_aggregate (33c5bb1) to 1e58928 (merge-base) diff using: array_agg
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                                                              HEAD                                   perf_array_aggregate
-----                                                                              ----                                   --------------------
array_agg i64 merge_batch 30% nulls, 0% of nulls point to a zero length array      1.02   508.6±10.52µs        ? ?/sec    1.00    497.7±6.50µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 100% of nulls point to a zero length array    1.00      4.6±0.00µs        ? ?/sec    1.00      4.6±0.00µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 50% of nulls point to a zero length array     1.02    510.3±9.30µs        ? ?/sec    1.00    502.0±6.26µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 90% of nulls point to a zero length array     1.01   510.2±10.92µs        ? ?/sec    1.00    504.1±5.99µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 99% of nulls point to a zero length array     1.03    515.3±8.71µs        ? ?/sec    1.00    501.2±5.86µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 0% of nulls point to a zero length array      1.02    218.6±3.77µs        ? ?/sec    1.00    213.5±2.63µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 100% of nulls point to a zero length array    1.00      4.4±0.00µs        ? ?/sec    1.00      4.4±0.00µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 50% of nulls point to a zero length array     1.00    215.9±3.92µs        ? ?/sec    1.00    216.5±2.39µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 90% of nulls point to a zero length array     1.01    218.1±5.02µs        ? ?/sec    1.00    217.0±2.31µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 99% of nulls point to a zero length array     1.02    220.0±4.23µs        ? ?/sec    1.00    216.5±2.32µs        ? ?/sec
array_agg i64 merge_batch all nulls, 100% of nulls point to a zero length array    1.01     73.6±3.29ns        ? ?/sec    1.00     73.1±3.08ns        ? ?/sec
array_agg i64 merge_batch all nulls, 90% of nulls point to a zero length array     1.02     74.0±3.03ns        ? ?/sec    1.00     72.4±2.89ns        ? ?/sec
array_agg i64 merge_batch no nulls                                                 1.00     81.6±1.37ns        ? ?/sec    1.01     82.6±1.19ns        ? ?/sec
distinct_array_agg utf8 high cardinality (~7800 distinct, 5% db names)                                                    1.00    274.1±0.84µs        ? ?/sec
distinct_array_agg utf8 low cardinality (~25 distinct)                                                                    1.00    147.5±0.52µs        ? ?/sec

Resource Usage

array_agg — base (merge-base)

Metric Value
Wall time 355.1s
Peak memory 27.7 MiB
Avg memory 8.3 MiB
CPU user 156.3s
CPU sys 0.1s
Peak spill 0 B

array_agg — branch

Metric Value
Wall time 370.1s
Peak memory 30.7 MiB
Avg memory 10.5 MiB
CPU user 180.9s
CPU sys 0.2s
Peak spill 0 B

File an issue against this benchmark runner

@gabotechs

Copy link
Copy Markdown
Contributor

Seems like there's no impact in those benchmarks. Do you know if there are any other benchmarks that are using array_agg? maybe tpch or tpcds?

@gabotechs

Copy link
Copy Markdown
Contributor

Let's ship the benchmarks in a preliminary PR, and then come back to this one.

@fred1268
fred1268 force-pushed the perf/array_aggregate branch from 33c5bb1 to 1cd1305 Compare July 21, 2026 09:02
@gabotechs

Copy link
Copy Markdown
Contributor

run benchmarks array_agg

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5032302654-1202-qdxqv 6.12.85+ #1 SMP Mon May 11 08:17:35 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing perf/array_aggregate (1cd1305) to 1fcdef2 (merge-base) diff using: array_agg
Results will be posted here when complete


File an issue against this benchmark runner

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                                                              HEAD                                   perf_array_aggregate
-----                                                                              ----                                   --------------------
array_agg i64 merge_batch 30% nulls, 0% of nulls point to a zero length array      1.00    502.5±4.80µs        ? ?/sec    1.00   501.6±10.91µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 100% of nulls point to a zero length array    1.00      4.6±0.00µs        ? ?/sec    1.00      4.6±0.00µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 50% of nulls point to a zero length array     1.01    507.6±5.23µs        ? ?/sec    1.00   504.5±11.24µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 90% of nulls point to a zero length array     1.01   509.7±12.25µs        ? ?/sec    1.00   507.1±11.09µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 99% of nulls point to a zero length array     1.00   504.8±10.50µs        ? ?/sec    1.01   509.2±10.60µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 0% of nulls point to a zero length array      1.00    214.2±2.30µs        ? ?/sec    1.00    214.5±4.55µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 100% of nulls point to a zero length array    1.00      4.4±0.00µs        ? ?/sec    1.00      4.4±0.01µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 50% of nulls point to a zero length array     1.00    215.4±2.38µs        ? ?/sec    1.01    217.2±4.56µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 90% of nulls point to a zero length array     1.00    215.5±2.38µs        ? ?/sec    1.01    216.8±4.77µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 99% of nulls point to a zero length array     1.00    215.6±2.63µs        ? ?/sec    1.01    217.9±4.56µs        ? ?/sec
array_agg i64 merge_batch all nulls, 100% of nulls point to a zero length array    1.00     72.1±2.71ns        ? ?/sec    1.04     74.7±3.17ns        ? ?/sec
array_agg i64 merge_batch all nulls, 90% of nulls point to a zero length array     1.00     72.2±2.70ns        ? ?/sec    1.11     80.2±4.36ns        ? ?/sec
array_agg i64 merge_batch no nulls                                                 1.00     81.8±1.32ns        ? ?/sec    1.01     82.4±1.25ns        ? ?/sec
distinct_array_agg utf8 high cardinality (~7800 distinct, 5% db names)             4.10   1121.5±2.78µs        ? ?/sec    1.00    273.2±0.68µs        ? ?/sec
distinct_array_agg utf8 low cardinality (~25 distinct)                             3.89    577.0±1.79µs        ? ?/sec    1.00    148.3±0.42µs        ? ?/sec

Resource Usage

array_agg — base (merge-base)

Metric Value
Wall time 260.1s
Peak memory 33.2 MiB
Avg memory 15.7 MiB
CPU user 181.2s
CPU sys 0.2s
Peak spill 0 B

array_agg — branch

Metric Value
Wall time 255.1s
Peak memory 30.9 MiB
Avg memory 15.5 MiB
CPU user 184.4s
CPU sys 0.2s
Peak spill 0 B

File an issue against this benchmark runner

@gabotechs

Copy link
Copy Markdown
Contributor

Nice!

distinct_array_agg utf8 high cardinality (~7800 distinct, 5% db names)             4.10   1121.5±2.78µs        ? ?/sec    1.00    273.2±0.68µs        ? ?/sec
distinct_array_agg utf8 low cardinality (~25 distinct)                             3.89    577.0±1.79µs        ? ?/sec    1.00    148.3±0.42µs        ? ?/sec

@gabotechs gabotechs changed the title array_agg() performance improvements perf: array_agg() performance improvements Jul 24, 2026
@fred1268

Copy link
Copy Markdown
Contributor Author

@jayzhan211 @alamb as the original author and reviewer of this code, you may be interested this PR.
(per @gabotechs recommandations, I created a separate PR to add tests and benchmarks before)

alamb
alamb previously approved these changes Jul 24, 2026

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fred1268 -- looks good to me amd is a nice improvement -- Rows will likely be much faster than ScalarValue as borne by the benchmarks

I (along with claude code) found two issues with this PR that were not picked up in regression tests:

  1. It breaks array_agg on dictionaries
  2. It has unbounded memory growth for retract_rows (aka when used as a window function)

I made a PR that adds test coverage here:

I also had a few style suggestions, but nothing critical.

I do think we need to fix the regressions though

assert_eq!(acc1.size(), 1684);
// The GroupValuesRows-based implementation uses a contiguous Rows
// buffer + HashTable instead of individual ScalarValue allocations,
// so the reported size differs from the previous implementation (was 1684).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the context about the previous impleementation will not be relevant after this PR merges -- it is more a comment for this PR -- just updating without this comment I think would be less confusing

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about this, I forgot to clean them up

// reads keys. Refcount semantics for retract are only valid within
// a single accumulator instance (window execution).
// The DISTINCT state is `List<value>`. Partial accumulators ship the
// set of live values, not multiplicities. Re-ingesting them here is

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are multiplicities? I think DISTINCT state is List<value> is probably enough for this comment (i see you dodn't add this)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about this, I forgot to clean them up

});
delayed_cmp_err?;
};
let group_values = self

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can likely avoid these expects / implicit state machines by using an explicit state enum . Maybe something like this:

enum state {
  Init,
  Allocated {
   map: ..., 
   group_values: ...: 
   converter: ...
  }
 }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented something that is my understanding of your comment.
Please let me know if it is not the case.

@alamb
alamb dismissed their stale review July 24, 2026 21:20

Clicked wrong button -- need to fix regressions first

athlcode pushed a commit to athlcode/datafusion that referenced this pull request Jul 25, 2026
…t_batch` memory (apache#23873)

## Which issue does this PR close?

- related to apache#23716


## Rationale for this change

It adds test coverage for two gaps found while reviewing
apache#23716.


## What changes are included in this PR?

Tests only, no functional change.
- Dictionary inputs
- memory usage on retract (make sure memory is released)

Note I moved `array_agg` cases out `aggregate.slt` as it is already more
than 9k lines long

## Are these changes tested?
They are only tests

## Are there any user-facing changes?

No. Tests only, no public API changes.

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>

/// Resources that are allocated lazily on the first `update_batch` call,
/// once the concrete runtime Arrow type is known.
///

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be amazing is if we could reuse some of the actual hash aggregate implementations -- but I don't really know how to do that yet

@alamb

alamb commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Thanks @fred1268 -- I merged up from main to resolve a conflict. The code looks good to me now, though it may be slower as it now does a new allocation for each distinct value (OwnedRow). I'll do the benchmarks one more time

@alamb

alamb commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

run benchmarks array_agg

@alamb alamb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fred1268 -- this looks good to me; As long as the performance results look good I think this one is good to merge.

Thanks again

/// `.expect()` calls that would otherwise be needed.
#[derive(Debug)]
pub struct DistinctArrayAggAccumulator {
struct DistinctState {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

group_values: Rows,
/// One owned encoded row per live distinct value, indexed by group index.
/// Compacted via swap-remove on eviction so there are never dead slots.
group_rows: Vec<OwnedRow>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this turns out to be too expenive (an allocation per OwnedRow) we could also use another Rows here, though managing it will be complicated as Rows doens't have a swap_remove function

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark running (GKE) | trigger
Instance: c4a-highmem-16 (12 vCPU / 65 GiB) | Linux bench-c5100859130-1280-9qnh9 6.12.85+ #1 SMP Mon May 11 08:17:35 UTC 2026 aarch64 GNU/Linux

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected

Comparing perf/array_aggregate (8389f2e) to bb75d92 (merge-base) diff using: array_agg
Results will be posted here when complete


File an issue against this benchmark runner

@fred1268

Copy link
Copy Markdown
Contributor Author

Thanks @alamb for the thorough review, and sorry if I pushed in two batches which was confusing for you (will try to do better next time). Let me know if there is something else I can do on this PR.

@adriangbot

Copy link
Copy Markdown

🤖 Benchmark completed (GKE) | trigger

Instance: c4a-highmem-16 (12 vCPU / 65 GiB)

CPU Details (lscpu)
Architecture:                            aarch64
CPU op-mode(s):                          64-bit
Byte Order:                              Little Endian
CPU(s):                                  16
On-line CPU(s) list:                     0-15
Vendor ID:                               ARM
Model name:                              Neoverse-V2
Model:                                   1
Thread(s) per core:                      1
Core(s) per cluster:                     16
Socket(s):                               -
Cluster(s):                              1
Stepping:                                r0p1
BogoMIPS:                                2000.00
Flags:                                   fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 sm3 sm4 asimddp sha512 sve asimdfhm dit uscat ilrcpc flagm sb paca pacg dcpodp sve2 sveaes svepmull svebitperm svesha3 svesm4 flagm2 frint svei8mm svebf16 i8mm bf16 dgh rng bti
L1d cache:                               1 MiB (16 instances)
L1i cache:                               1 MiB (16 instances)
L2 cache:                                32 MiB (16 instances)
L3 cache:                                80 MiB (1 instance)
NUMA node(s):                            1
NUMA node0 CPU(s):                       0-15
Vulnerability Gather data sampling:      Not affected
Vulnerability Indirect target selection: Not affected
Vulnerability Itlb multihit:             Not affected
Vulnerability L1tf:                      Not affected
Vulnerability Mds:                       Not affected
Vulnerability Meltdown:                  Not affected
Vulnerability Mmio stale data:           Not affected
Vulnerability Reg file data sampling:    Not affected
Vulnerability Retbleed:                  Not affected
Vulnerability Spec rstack overflow:      Not affected
Vulnerability Spec store bypass:         Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:                Mitigation; __user pointer sanitization
Vulnerability Spectre v2:                Mitigation; CSV2, BHB
Vulnerability Srbds:                     Not affected
Vulnerability Tsa:                       Not affected
Vulnerability Tsx async abort:           Not affected
Vulnerability Vmscape:                   Not affected
Details

group                                                                              HEAD                                   perf_array_aggregate
-----                                                                              ----                                   --------------------
array_agg i64 merge_batch 30% nulls, 0% of nulls point to a zero length array      1.00    501.4±6.42µs        ? ?/sec    1.00    500.5±5.68µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 100% of nulls point to a zero length array    1.00      4.6±0.00µs        ? ?/sec    1.00      4.6±0.01µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 50% of nulls point to a zero length array     1.00    504.0±6.34µs        ? ?/sec    1.00    502.3±5.59µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 90% of nulls point to a zero length array     1.19    592.2±6.54µs        ? ?/sec    1.00    499.5±5.91µs        ? ?/sec
array_agg i64 merge_batch 30% nulls, 99% of nulls point to a zero length array     1.01    509.4±6.23µs        ? ?/sec    1.00    505.2±5.59µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 0% of nulls point to a zero length array      1.00    216.2±2.71µs        ? ?/sec    1.00    216.4±2.41µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 100% of nulls point to a zero length array    1.00      4.4±0.00µs        ? ?/sec    1.00      4.4±0.00µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 50% of nulls point to a zero length array     1.00    214.1±2.68µs        ? ?/sec    1.00    214.2±2.47µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 90% of nulls point to a zero length array     1.00    216.6±2.73µs        ? ?/sec    1.00    215.7±2.38µs        ? ?/sec
array_agg i64 merge_batch 70% nulls, 99% of nulls point to a zero length array     1.01    217.0±2.74µs        ? ?/sec    1.00    214.7±2.68µs        ? ?/sec
array_agg i64 merge_batch all nulls, 100% of nulls point to a zero length array    1.04     75.3±3.51ns        ? ?/sec    1.00     72.4±2.93ns        ? ?/sec
array_agg i64 merge_batch all nulls, 90% of nulls point to a zero length array     1.28     94.0±2.91ns        ? ?/sec    1.00     73.2±2.96ns        ? ?/sec
array_agg i64 merge_batch no nulls                                                 1.00     82.1±2.50ns        ? ?/sec    1.00     82.4±1.42ns        ? ?/sec
distinct_array_agg utf8 high cardinality (~7800 distinct, 5% db names)             1.68  1126.6±26.64µs        ? ?/sec    1.00    672.3±1.24µs        ? ?/sec
distinct_array_agg utf8 low cardinality (~25 distinct)                             3.85    586.2±9.97µs        ? ?/sec    1.00    152.3±0.58µs        ? ?/sec

Resource Usage

array_agg — base (merge-base)

Metric Value
Wall time 350.1s
Peak memory 35.1 MiB
Avg memory 12.2 MiB
CPU user 182.0s
CPU sys 0.2s
Peak spill 0 B

array_agg — branch

Metric Value
Wall time 345.1s
Peak memory 29.8 MiB
Avg memory 11.3 MiB
CPU user 185.1s
CPU sys 0.2s
Peak spill 0 B

File an issue against this benchmark runner

@fred1268

Copy link
Copy Markdown
Contributor Author

The high cardinality benchmark, although still an improvement, is not as good as in the previous version. That being said, I would expect most people will do array_agg(DISTINCT col) when there is a limited number of values in col so this should not be too much of a problem.

@alamb

alamb commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Performance results definitely look like an improvement to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

functions Changes to functions implementation performance Make DataFusion faster

Projects

None yet

Development

Successfully merging this pull request may close these issues.

array_agg() performance

5 participants